/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       toggle_button.cpp

	$Header: /game/toggle_button.cpp $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "toggle_button.h"

#include "mouse_event.h"

// ----------------------------------------------------------------------------
// button which switches between pressed / released states.  If "sticky", button
// cannot leave the "pressed" state unless the program sets that state.
// -----------------------------------------------------------------------------
	// constructors
// ----------------------------------------------------------------------------
// construct with a standard set of bitmaps, from a bitmap_group
// ----------------------------------------------------------------------------
t_toggle_button::t_toggle_button( t_cached_ptr<t_button_bitmaps>& bitmaps, t_screen_point point,
		                          t_window* parent, bool sticky, std::string const& text )
			   : t_button( bitmaps, point, parent, text )
{
	m_sticky = sticky;
}

// ----------------------------------------------------------------------------
    // start with no images.  Must set images manually.
// ----------------------------------------------------------------------------
t_toggle_button::t_toggle_button( t_screen_point point, t_window* parent, bool sticky )
               : t_button( point, parent )
{
	m_sticky = sticky;
}

// ----------------------------------------------------------------------------
// handle a left-button down event
// ----------------------------------------------------------------------------
void t_toggle_button::left_button_down( t_mouse_event const& event )
{
    if (m_disabled)
        return;
	if (m_sticky && m_pressed)
		return;
    m_pressed = !m_pressed;
	set_image();
	m_clicked( this );
}

// ----------------------------------------------------------------------------
// handle a left-button up event.  Do nothing.
// ----------------------------------------------------------------------------
void t_toggle_button::left_button_up( t_mouse_event const& event )
{
}

// ----------------------------------------------------------------------------
// clear the highlight state when mouse leaves
// ----------------------------------------------------------------------------
void t_toggle_button::mouse_leaving( t_window* old_window, t_window* new_window,
									 t_mouse_event const& event )
{
	if (is_child( new_window ))
		return;

	if (m_highlighted)
	{
	    m_highlighted = false;
		set_image();
	}
}

// ----------------------------------------------------------------------------
// set / clear the pressed state
// ----------------------------------------------------------------------------
void t_toggle_button::set_pressed( bool arg )
{
	if (m_pressed == arg)
		return;

	m_pressed = arg;
	set_image();
	m_clicked( this );
}
